<template>
  <div class="app-container">
    <div class="filter-container">
      <!--<el-select v-model="listQuery.wxAccountId" placeholder="请选择公众号" clearable class="filter-item" style="width: 200px;margin-right: 20px">
        <el-option v-for="item in accountListOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>-->
      <el-input
        v-model="listQuery.wNickname"
        :placeholder="'请输入外观名称'"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        class="filter-item"
        v-model="listQuery.beginDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="开始日期">
      </el-date-picker>
      <el-date-picker
        class="filter-item"
        v-model="listQuery.endDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="结束日期">
      </el-date-picker>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-refresh" @click="handleRefresh">
        刷新
      </el-button>
<!--      <el-button-->
<!--        v-has="'jiansanPriceList:add'"-->
<!--        v-waves-->
<!--        class="filter-item"-->
<!--        style="margin-left: 10px;"-->
<!--        type="primary"-->
<!--        icon="el-icon-plus"-->
<!--        @click="handleCreate"-->
<!--      >-->
<!--        {{ $t('table.add') }}-->
<!--      </el-button>-->
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column :label="tempDesc.wName" >
        <template slot-scope="scope">
          <span>{{ scope.row.wName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.wNickname">
        <template slot-scope="scope">
          <span>{{ scope.row.wNickname }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.wPic">
        <template slot-scope="scope">
          <span>{{ scope.row.wPic }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.wDate" width="160px">
        <template slot-scope="scope">
          <span>{{ scope.row.wDate}}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.wPriceDxw" width="160px">
        <template slot-scope="scope">
          <span>{{ scope.row.wPriceDxw  }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.wPriceWty" width="160px">
        <template slot-scope="scope">
          <span>{{ scope.row.wPriceWty  }}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="checkBtnPermission(['jiansanPriceList:add','jiansanPriceList:edit','jiansanPriceList:delete'])" :label="$t('table.actions')" width="300px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-has="'jiansanPriceList:add'" type="primary" size="mini" icon="el-icon-plus" @click="handleCreate(scope.row)">
            新增
          </el-button>
          <el-button v-has="'jiansanPriceList:edit'" type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">
            编辑
          </el-button>
          <el-button v-has="'jiansanPriceList:delete'" type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin-left:50px;"
      >
        <!--<el-form-item :label="tempDesc.wxAccountId" prop="wxAccountId">
          <el-select v-model="temp.wxAccountId" placeholder="请选择公众号" clearable class="filter-item" style="width: 280px;" :disabled="dialogStatus !='create'">
            <el-option v-for="item in accountListOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>-->
        <el-form-item :label="tempDesc.wName" prop="wName">
          <el-input v-model="temp.wName" :placeholder="'请输入' + tempDesc.wName" disabled="true" />
        </el-form-item>
        <el-form-item :label="tempDesc.wNickname" prop="wNickname">
          <el-input v-model="temp.wNickname" :placeholder="'请输入' + tempDesc.wNickname" disabled="true" />
        </el-form-item>
        <el-form-item :label="tempDesc.wDate" prop="wDate">
          <el-input v-model="temp.wDate" :placeholder="'请输入' + tempDesc.wDate" />
        </el-form-item>
        <el-form-item :label="tempDesc.wPriceDxw" prop="wPriceDxw">
          <el-input v-model="temp.wPriceDxw" :placeholder="'请输入' + tempDesc.wPriceDxw" />
        </el-form-item>
        <el-form-item :label="tempDesc.wPriceWty" prop="wPriceWty">
          <el-input v-model="temp.wPriceWty" :placeholder="'请输入' + tempDesc.wPriceWty" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { checkBtnPermission } from '@/utils/permission' // 权限判断函数
import { fetchList, createRow, updateRow, deleteRow } from '@/api/w-price'
import { fetchAllAccount } from '@/api/wx-account'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

// 常量title
const tempDesc = {
  'wId': '',
  'wName': '外观官方名称',
  'wNickname': '外观昵称',
  'wDate': '时间',
  'wPriceDxw': '电五价格',
  'wPriceWty': '网通价格',
  'wPic': '外观图片'
}
export default {
  // name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        wNickname: '',
        beginDate: '',
        endDate: ''
      },
      temp: {
        'wId': '',
        'wName': '',
        'wNickname': '',
        'wDate': '',
        'wPriceDxw': '',
        'wPriceWty': '',
        'wPic': ''
      },
      tempDesc: tempDesc,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '增加'
      },
      rules: {
        wName: [{ required: true, message: '请输入' + tempDesc.wName, trigger: 'blur' }],
        wNickname: [{ required: true, message: '请输入' + tempDesc.wNickname, trigger: 'blur' }],
        wDate: [{ required: true, message: '请输入' + tempDesc.wDate, trigger: 'blur' }],
        wPriceDxw: [{ required: true, message: '请输入' + tempDesc.wPriceDxw, trigger: 'blur' }],
        wPriceWty: [{ required: true, message: '请输入' + tempDesc.wPriceWty, trigger: 'blur' }]
      },
      accountListOptions: [
      ],
      fansOptions: [],
      loading: false,
      fansSelected: []
    }
  },
  created() {
    this.listQuery.wNickname = this.$route.params.id
    this.getList()
    this.getAccountList()
  },
  methods: {
    checkBtnPermission,
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleRefresh() {
      this.listQuery.wNickname = ''
      this.listQuery.beginDate = ''
      this.listQuery.endDate = ''
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        'wPid': '',
        'wObjid': '',
        'wName': '',
        'wNickname': '',
        'wDate': '',
        'wPriceDxw': '',
        'wPriceWty': '',
        'wPic': ''
      }
    },
    handleCreate(row) {
      this.temp = Object.assign({}, row)
      this.temp.wPid = ''
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createRow(this.temp).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateRow(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      if (row.isRelatedMenu === '1') {
        this.$message({ message: '请先解除关联', type: 'warning' })
        return false
      }
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          var params = { 'id': parseInt(row.wPid) }
          deleteRow(params).then(() => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
    },
    getAccountList() {
      fetchAllAccount().then(response => {
        this.accountListOptions = []
        for (var i = 0; i < response.data.length; i++) {
          this.accountListOptions.push({ 'key': response.data[i].id + '', 'display_name': response.data[i].name })
        }
      })
    },
    handleUpload(row) {
      this.$confirm('确定上传图文到微信?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = { 'id': row.id }
        deleteRow(params).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    },
    formatNewsType(row, column) {
      if (row.countArticle === 0) {
        return ''
      }
      if (row.countArticle === 1) {
        return '单图文'
      }
      if (row.countArticle > 1) {
        return '多图文'
      }
    }
  }
}
</script>
